using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using AForge.Imaging.Filters;

	/// <summary>
	/// Summary description for PixelateForm.
	/// </summary>
	public class PixelateForm : System.Windows.Forms.Form
	{
		private Pixellate filter = new Pixellate(8);
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox sizeBox;
		private System.Windows.Forms.TrackBar trackBar;
		private System.Windows.Forms.GroupBox groupBox1;
		private FilterPreview filterPreview;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Button okButton;
		private System.Windows.Forms.Button cancelButton;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		// Image property
		public Bitmap Image
		{
			set { filterPreview.Image = value; }
		}
		// Filter property
		public IFilter Filter
		{
			get { return filter; }
		}

		// Constructor
		public PixelateForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			sizeBox.Text = filter.PixelSize.ToString();
			trackBar.Value = filter.PixelSize;

			filterPreview.Filter = filter;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.sizeBox = new System.Windows.Forms.TextBox();
			this.trackBar = new System.Windows.Forms.TrackBar();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.filterPreview = new FilterPreview();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.okButton = new System.Windows.Forms.Button();
			this.cancelButton = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.trackBar)).BeginInit();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(10, 18);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(60, 14);
			this.label1.TabIndex = 0;
			this.label1.Text = "Pixel size:";
			// 
			// sizeBox
			// 
			this.sizeBox.Location = new System.Drawing.Point(70, 15);
			this.sizeBox.Name = "sizeBox";
			this.sizeBox.ReadOnly = true;
			this.sizeBox.TabIndex = 1;
			this.sizeBox.TabStop = false;
			this.sizeBox.Text = "";
			this.sizeBox.TextChanged += new System.EventHandler(this.sizeBox_TextChanged);
			// 
			// trackBar
			// 
			this.trackBar.Location = new System.Drawing.Point(10, 45);
			this.trackBar.Maximum = 32;
			this.trackBar.Minimum = 2;
			this.trackBar.Name = "trackBar";
			this.trackBar.Size = new System.Drawing.Size(200, 42);
			this.trackBar.TabIndex = 2;
			this.trackBar.Value = 2;
			this.trackBar.Scroll += new System.EventHandler(this.trackBar_Scroll);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.filterPreview});
			this.groupBox1.Location = new System.Drawing.Point(230, 10);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(170, 175);
			this.groupBox1.TabIndex = 3;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Preview";
			// 
			// filterPreview
			// 
			this.filterPreview.Image = null;
			this.filterPreview.Location = new System.Drawing.Point(10, 15);
			this.filterPreview.Name = "filterPreview";
			this.filterPreview.Size = new System.Drawing.Size(150, 150);
			this.filterPreview.TabIndex = 0;
			this.filterPreview.TabStop = false;
			// 
			// pictureBox1
			// 
			this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.pictureBox1.Location = new System.Drawing.Point(10, 150);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(210, 2);
			this.pictureBox1.TabIndex = 4;
			this.pictureBox1.TabStop = false;
			// 
			// okButton
			// 
			this.okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.okButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.okButton.Location = new System.Drawing.Point(35, 160);
			this.okButton.Name = "okButton";
			this.okButton.TabIndex = 5;
			this.okButton.Text = "Ok";
			// 
			// cancelButton
			// 
			this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cancelButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.cancelButton.Location = new System.Drawing.Point(120, 160);
			this.cancelButton.Name = "cancelButton";
			this.cancelButton.TabIndex = 6;
			this.cancelButton.Text = "Cancel";
			// 
			// PixelateForm
			// 
			this.AcceptButton = this.okButton;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.cancelButton;
			this.ClientSize = new System.Drawing.Size(409, 193);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.cancelButton,
																		  this.okButton,
																		  this.pictureBox1,
																		  this.groupBox1,
																		  this.trackBar,
																		  this.sizeBox,
																		  this.label1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "PixelateForm";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Pixelate";
			((System.ComponentModel.ISupportInitialize)(this.trackBar)).EndInit();
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		// Pixel size changed
		private void sizeBox_TextChanged(object sender, System.EventArgs e)
		{
			try
			{
				filter.PixelSize = Math.Max(2, Math.Min(32, int.Parse(sizeBox.Text)));
				filterPreview.RefreshFilter();
			}
			catch (Exception)
			{
			}
		}

		// Trackbar scrolled
		private void trackBar_Scroll(object sender, System.EventArgs e)
		{
			sizeBox.Text = trackBar.Value.ToString();
		}
	}
